package com.eastdigit.shiro;

import com.eastdigit.system.model.Constants;
import com.eastdigit.system.model.Menu;
import com.eastdigit.system.service.IPermissionService;
import com.eastdigit.util.StringUtil;
import org.apache.shiro.config.Ini;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Set;

/**
 * Created by acer on 14-2-11.
 */
public class ChainDefinitionSectionMetaSource implements FactoryBean<Ini.Section> {

    private String filterChainDefinitions;

    @Autowired
    private IPermissionService permissionService;

    @Autowired(required = false)
    private CustomChainDefinition customChainDefinition;

    public static Set<String> filters;

    @Override
    public Ini.Section getObject() throws BeansException {
        Ini.Section section = null;
        if (customChainDefinition != null) {
            section = customChainDefinition.getObject();
        }
        Ini ini = new Ini();
        // 加载默认的url
        ini.load(filterChainDefinitions);
        Ini.Section defaultSection = ini.getSection(Ini.DEFAULT_SECTION_NAME);
        if (section == null) {
            section = defaultSection;
        } else {
            for (String url : defaultSection.keySet()) {
                section.put(url, defaultSection.get(url));
            }
        }

        // 查询所有权限,逐个添加到section中。section就是filterChainDefinitionMap,
        // 里面的键就是链接URL,值就是存在什么条件才能访问该链接
        // 获取所有菜单，功能点
        for (Menu menu : permissionService.queryAllMenuAndFunctions()) {
            if (Constants.MENU_TYPE_FUNCTION.equals(menu.getType())) {// 功能点
                if (!StringUtil.isNull(menu.getUrl())) {
                    section.put(menu.getUrl(), ShiroConstants.PREMISSION_STATE);
                }
            } else {// 菜单
                if (!StringUtil.isNull(menu.getCtrlPath())) {
                    section.put("/js/controllers/" + menu.getCtrlPath(), ShiroConstants.PREMISSION_STATE);
                }
                if (!StringUtil.isNull(menu.getTemplateUrl())) {
                    section.put("/views/" + menu.getTemplateUrl(), ShiroConstants.PREMISSION_STATE);
                }
            }
        }
        // 添加默认控制权限
        section.put("/**", "authc");

        filters = section.keySet();

        return section;
    }

    /**
     * 通过filterChainDefinitions对默认的url过滤定义
     * 
     * @param filterChainDefinitions
     *            默认的url过滤定义
     */
    public void setFilterChainDefinitions(String filterChainDefinitions) {
        this.filterChainDefinitions = filterChainDefinitions;
    }

    public void setPermissionService(IPermissionService permissionService) {
        this.permissionService = permissionService;
    }

    public void setCustomChainDefinition(CustomChainDefinition customChainDefinition) {
        this.customChainDefinition = customChainDefinition;
    }

    @Override
    public Class<?> getObjectType() {
        return this.getClass();
    }

    @Override
    public boolean isSingleton() {
        return false;
    }

    public static boolean isFiltered(String permission) {
        return filters.contains(permission);
    }

}
